package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.AdminMapper;
import com.cskaoyan.catmall.model.bo.AdminCreateBo;
import com.cskaoyan.catmall.model.bo.AdminDeleteBo;
import com.cskaoyan.catmall.model.bo.AdminListBo;
import com.cskaoyan.catmall.model.bo.AdminUpdateBo;
import com.cskaoyan.catmall.model.vo.admin.AdminCreateVo;
import com.cskaoyan.catmall.model.vo.admin.AdminListVo;
import com.cskaoyan.catmall.model.vo.admin.AdminUpdateVo;
import com.cskaoyan.catmall.service.AdminService;
import com.cskaoyan.catmall.utils.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022-01-08-22-59
 * author : WQW
 * description: 对数据库中的管理员信息进行增删改查
 */
@Service
public class AdminServiceImpl implements AdminService {


    @Autowired
    AdminMapper adminMapper;


    /**
     * @param
     * @param adminListBo
     * @return com.cskaoyan.catmall.model.vo.admin.AdminListVo
     * @description 查询所有管理员信息并返回
     * @author WQW
     * @date 2022/1/8 23:04
     */
    @Override
    public AdminListVo selectAdminList(AdminListBo adminListBo) {
        Integer page = adminListBo.getPage();
        Integer limit = adminListBo.getLimit();
        PageHelper.startPage(page, limit);
        List<AdminListVo.ListDTO> listDTOS = adminMapper.selectAllAdmin(adminListBo);
        PageInfo<AdminListVo.ListDTO> pageInfo = new PageInfo<>(listDTOS);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return new AdminListVo(total, pages, limit, page, listDTOS);
    }


    /**
     * @description
     * @author WQW
     * @date 2022/1/10 10:23
     * @param
     * @param adminCreateBo
     * @return com.cskaoyan.catmall.model.vo.admin.AdminCreateVo
     */
    @Override
    @Transactional
    public AdminCreateVo addNewAdmin(AdminCreateBo adminCreateBo) {
        AdminCreateVo adminCreateVo = new AdminCreateVo();
        List<String> nameList = adminMapper.selectAllAdminName(null);
        if (nameList.contains(adminCreateBo.getUsername())) {
            return adminCreateVo;
        }
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String format = dateFormat.format(date);
        adminCreateVo.setUsername(adminCreateBo.getUsername());
        adminCreateVo.setPassword(MD5Util.encrypt(adminCreateBo.getPassword()));
        adminCreateVo.setAvatar(adminCreateBo.getAvatar());
        adminCreateVo.setRoleIds(adminCreateBo.getRoleIds());
        adminCreateVo.setAddTime(format);
        adminCreateVo.setUpdateTime(format);
        adminMapper.insertAdmin(adminCreateVo);
        return adminCreateVo;
    }


    /**
     * @description
     * @author WQW
     * @date 2022/1/10 10:23
     * @param
     * @param adminDeleteBo
     * @return int
     */
    @Override
    @Transactional
    public int deleteAdmin(AdminDeleteBo adminDeleteBo) {
        try {
            adminMapper.deleteAdmin(adminDeleteBo.getId());
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 404;
        }
        return 200;
    }

    /**
     * @description
     * @author WQW
     * @date 2022/1/10 10:23
     * @param
     * @param adminUpdateBo
     * @return com.cskaoyan.catmall.model.vo.admin.AdminUpdateVo
     */
    @Override
    @Transactional
    public AdminUpdateVo updateAdmin(AdminUpdateBo adminUpdateBo) {
        AdminUpdateVo adminUpdateVo = null;
        List<String> nameList = adminMapper.selectAllAdminName(adminUpdateBo.getId());
        if (nameList.contains(adminUpdateBo.getUsername())) {
            return adminUpdateVo;
        }
        try {
            adminMapper.updateAdmin(adminUpdateBo);
            adminUpdateVo = adminMapper.selectAdminById(adminUpdateBo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new AdminUpdateVo();
        }
        return adminUpdateVo;
    }

    /**
     * @description 登陆比对密码
     * @author ghr
     * @date 2022/1/10 6:17 下午
     * @param
     * @param username
     * @return java.util.List<java.lang.String>
     */
    @Override
    public List<String> selectPasswordByName(String username) {
       List<String> passwords = adminMapper.selectPasswordByUsername(username);
       return passwords;
    }

    /**
     * @description 查找管理头像
     * @author ghr
     * @date 2022/1/10 11:00 下午
     * @param
     * @param principal
     * @return java.lang.String
     */
    @Override
    public String selectAvatar(String principal) {
        String avatar = adminMapper.selectAvatar(principal);
        return avatar;
    }

    /**
     * @description 查找管理员角色
     * @author ghr
     * @date 2022/1/10 11:04 下午
     * @param
     * @param principal
     * @return java.lang.String
     */
    @Override
    public String selectRole(String principal) {
        Integer[] roleIds = adminMapper.selectRoleId(principal);
        int roleId = 10;
        for (Integer id : roleIds) {
            if (id < roleId){
                roleId = id;
            }
        }
        String role = adminMapper.selectRole(roleId);
        return role;
    }

    /**
     * @description 管理员权限
     * @author ghr
     * @date 2022/1/10 11:14 下午
     * @param
     * @param role
     * @return java.util.List<java.lang.String>
     */
    @Override
    public List<String> selectPerms(String principal) {
        Integer[] roleIds = adminMapper.selectRoleId(principal);
        int roleId = 10;
        for (Integer id : roleIds) {
            if (id < roleId){
                roleId = id;
            }
        }
        List<String> list = adminMapper.selectPerms(roleId);
        return list;
    }

    @Override
    public List<Integer> selectAdminByUsername(String username) {
        return adminMapper.selectAdminByUsername(username);
    }

    @Override
    public List<String> selectUserPerms(String[] list) {
        return adminMapper.selectUserPerms(list);
    }

    @Override
    public List<String> selectUserRoles(String[] list) {
        return adminMapper.selectUserRoles(list);
    }
}
